package com.example.rabbitmq.demo1_delay;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author songyahui
 * @Date 2023/7/12 7:42
 * @Description
 */
@Configuration
public class TtlQueueConfig {

    //普通交换机
    public static final String X_EXCHANGE = "X";
    //死信交换机
    public static final String Y_DEAD_LETTER_EXCHANGE = "Y";
    //普通队列
    public static final String QUEUE_A = "QA";
    public static final String QUEUE_B = "QB";
    //死信队列
    public static final String DEAD_LETTER_QUEUE = "QD";

    //声明X交换机
    @Bean("xExchange")
    public DirectExchange xExchange(){
        return new DirectExchange(X_EXCHANGE);
    }

    //声明y交换机
    @Bean("yExchange")
    public DirectExchange yExchange(){
        return new DirectExchange(Y_DEAD_LETTER_EXCHANGE);
    }

    //声明队列
    @Bean("queueA")
    public Queue queueA(){
        Map<String, Object> arguments = new HashMap<>(3);
        //设置死信交换机
        arguments.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE);
        //设置死信routing key
        arguments.put("x-dead-letter-routing-key","YD");
        //设置ttl
        arguments.put("x-message-ttl",10000);

        return QueueBuilder.durable(QUEUE_A).withArguments(arguments).build();
    }

    //声明队列
    @Bean("queueB")
    public Queue queueB(){
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE);
        arguments.put("x-dead-letter-routing-key", "YD");
        arguments.put("x-message-ttl", 40000);
        return QueueBuilder.durable(QUEUE_B).withArguments(arguments).build();
    }

    //声明队列
    @Bean("queueD")
    public Queue queueD(){
        Map<String, Object> arguments = new HashMap<>();
        return QueueBuilder.durable(DEAD_LETTER_QUEUE).build();
    }

    //绑定exchange和queue
    @Bean
    public Binding queueA_BindingX(@Qualifier("queueA") Queue queueA, @Qualifier("xExchange") DirectExchange x_exchange) {
        return BindingBuilder.bind(queueA).to(x_exchange).with("XA");

    }

    @Bean
    public Binding queueB_BindingX(@Qualifier("queueB") Queue queueB, @Qualifier("xExchange") DirectExchange x_exchange){
        return BindingBuilder.bind(queueB).to(x_exchange).with("XB");
    }

    @Bean
    public Binding queueD_BindingY(@Qualifier("queueD") Queue queueD, @Qualifier("yExchange") DirectExchange y_exchange){
        return BindingBuilder.bind(queueD).to(y_exchange).with("YD");
    }
}
